package com.wiegand.loader {
	import com.wiegand.error.ErrorHandler;
	import com.wiegand.event.LoaderEvent;

	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.Font;

	/**
	 * @author kwiegand
	 */
	 
	public class FontLoader extends EventDispatcher{

		private static var _instance : FontLoader;
		
		private var _fontPackage : String = "";
		
		private var _fontPath : String = "";
					
		private var _loader : Loader;
		
		private var _fontsToLoad : Array;
		
		private var _loadedFonts : int = 0;	
		
		public static function getInstance() : FontLoader{
			
			if( _instance == null )
				_instance = new FontLoader( new SingletonEnforecer() );
			
			return _instance;
			
		}
		
		public function FontLoader( blocker:SingletonEnforecer ):void{
			
			if ( blocker == null ) {
				
            	throw new Error("Error: Instantiation failed: Use FontLoader.getInstance() instead of new.");
          
          	}
          	else{
          	
          		init();
          	
          	}
		
		}
		
		private function init() : void{
			
			_fontsToLoad = new Array();
			
			_loader = new Loader();				
			_loader.contentLoaderInfo.addEventListener( Event.INIT, handleComplete );
			_loader.contentLoaderInfo.addEventListener( ErrorEvent.ERROR, errorHandler );
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, errorHandler );
			
		}
		
		private function errorHandler( e: ErrorEvent) :void{
				
			ErrorHandler.getInstance().show(4);
			
		}
		
		public function load( fontPackage : String, fontArray : String, fontPath : String ):void{
		
			_fontsToLoad = fontArray.split(",");
			
			_fontPackage = fontPackage;
			
			_fontPath = fontPath;
								
			dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_START_LOADING ) );				
				
			loadFile( _loadedFonts );	
		}
		
		private function loadFile( n : int ): void{
					
			var file : String = _fontPath + _fontsToLoad[ n ] + ".swf";	
			
			trace("load file: " + file);	
					
			_loader.load( new URLRequest( file ) );	
		
		}
		
		private function handleComplete( e : Event ) : void {				
					
			try{

				var no : int = _loadedFonts;
											
				var className : String = String( _fontsToLoad[ no ] );
				var completeClassName : String = _fontPackage + className;
								
//							trace("------------------");
//							trace("------------------");
//							trace("_fontPackage: "+_fontPackage);
//							trace("_fontPath: "+_fontPath);
//							trace("className: "+className);
//							trace("completeClassName: "+completeClassName);
//							trace("------------------");
//							trace("------------------");
							
							if( className.substring( className.length - 3  ) == "_st" ){
							 
								//bitmap font from fla library
							 
								if( e.target.applicationDomain.hasDefinition( className ) ){
								
									var font : Class = e.target.applicationDomain.getDefinition( className ) as Class;
				
				                    Font.registerFont( font );
				        			
//				        			trace( "FontLoader: " + className + " registered." );
				        			
				        			_loadedFonts++;
				
				        			if( _loadedFonts < _fontsToLoad.length ) loadFile( _loadedFonts );	
				        			else dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ) );	
				        						
								}
								else trace( "FontLoader: ERROR - FONTCLASS '"+className+"' DON'T EXIST!" );
															 
							}
							else{
							
								//normal font from as3 class
								
								if( e.target.applicationDomain.hasDefinition( completeClassName ) ){
								
									var FontLibrary : Class = e.target.applicationDomain.getDefinition( completeClassName ) as Class;
				
				                    Font.registerFont( FontLibrary[ className ] );
				        			
//				        			trace( "FontLoader: " + className + " registered." );
				        			
				        			_loadedFonts++;
				
				        			if( _loadedFonts < _fontsToLoad.length ) loadFile( _loadedFonts );	
				        			else dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ) );	
				        						
								}
								else trace( "FontLoader: ERROR - FONTCLASS '"+className+"' DON'T EXIST!" );
							
							}	
																				
			}
			catch ( e : TypeError){
				
				ErrorHandler.getInstance().show(4);
				
			}
			
		}

		
		public function clear():void{
						
			_loader = null;
		
		}
		
		
	}
}

internal class SingletonEnforecer {}
